import openpyxl
import random

# 第一步: 创建工作簿
wb = openpyxl.Workbook()
# 第二步: 添加工作表
sheet = wb.active
sheet.title = "测试成绩"

# 第三步: 写入数据
titles = ("姓名", "读书", "骑马", "射箭")
# 行和列的索引全部从1开始 
# 将标题写入到文件中
for col_index, title in enumerate(titles):
    sheet.cell(1, col_index + 1, title)

names = ("天偿", "圆寂", "支勋", "渣渣辉", "润邦")
# 将姓名和成绩写入到文件中
for row_index, name in enumerate(names):
    sheet.cell(row_index + 2, 1, name)
    for col_index in range(2, 5):
        sheet.cell(row_index + 2, col_index, random.randrange(50, 101))

# 第四步: 保存工作簿
wb.save("期末测试成绩.xlsx")
